/*
 * Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary information
 * of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.web.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yunji.framework_template.common.constant.Constant;
import com.yunji.framework_template.common.util.CookieUtil;
import com.yunji.framework_template.common.util.StringUtil;

/**
 * ClassName:LocalLanguageFilter <br/>
 * Date: 2016年1月27日 下午5:39:10 <br/>
 * 
 * @author fenglibin
 * @version
 * @see
 */
public class LocalLanguageFilter implements Filter {

    private ServletContext ctx;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ctx = filterConfig.getServletContext();

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                                                                                              ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        Map<String, String> cookies = CookieUtil.getCookies(req);
        String currentLocale = cookies.get("currentLocale");

        if (StringUtil.isEmpty(currentLocale)) {
            Cookie localeCookie = new Cookie("alertVersionCookie", "alertVersionCookie");
            // 一年有效
            localeCookie.setMaxAge(60 * 60 * 24 * 365);
            localeCookie.setPath("/");
            res.addCookie(localeCookie);
        } else {
            CookieUtil.removeCookie(req, res, "alertVersionCookie");
        }

        if (!("sc".equals(currentLocale) || "tc".equals(currentLocale) || "en".equals(currentLocale))) {// 不是这三种语言中的任何一种，默认为英语
            currentLocale = "en";
        }
        Cookie localeCookie = new Cookie("currentLocale", currentLocale);
        // 一年有效
        localeCookie.setMaxAge(60 * 60 * 24 * 365);
        localeCookie.setPath("/");

        res.addCookie(localeCookie);

        request.setAttribute("currentLocale", currentLocale);
        if (!StringUtil.isEmpty(cookies.get(Constant.USER_SESSION_UID))) {
            request.setAttribute(Constant.LOGIN_TAG_KEY.toString(), "console");
            request.setAttribute(Constant.LOGIN_MSG_URL_KEY, "#/console/internal-message/inbox");
        } else if (!StringUtil.isEmpty(cookies.get(Constant.ADMIN_SESSION_UID))) {
            request.setAttribute(Constant.LOGIN_TAG_KEY.toString(), "admin");
            request.setAttribute(Constant.LOGIN_MSG_URL_KEY, "/admin/internal-message.html");
        }

        /* 是否允许Json跨域调用，加上以下的配置表示允许跨域调用 */
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Credentials", "true");
        res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT,DELETE");

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}
